package com.lpf.handler1.client;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public class MyClientHandler extends SimpleChannelInboundHandler<Long> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Long msg) throws Exception {
        System.out.println(ctx.channel().remoteAddress() + ": " + msg);
    }

    // 在这里触发最开始的消息发送
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        ctx.writeAndFlush(123456L);
        //ctx.writeAndFlush(1L);
        //ctx.writeAndFlush(2L);
        //ctx.writeAndFlush(3L);
        //ctx.writeAndFlush(4L);

        // 绕过 MyLongToByteEncoder、MyByteToLongDecoder的处理将消息发送出去
        // 到服务端后会按照Long的形式输出（经过MyByteToLongDecoder解码器）
        //ctx.writeAndFlush(Unpooled.copiedBuffer("helloworld", CharsetUtil.UTF_8));
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}